<template>
  <div class="components-rich-text">
    <div class="rich-text" v-html="html" @click="clickHandler"></div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from "vue";
import hljs from "highlight.js";
import "highlight.js/styles/atom-one-dark.css";

export default defineComponent({
  name: "RichText",
  props: {
    text: {
      type: String,
      default: "",
    },
  },
  setup(props) {
    const html = computed(() => {
      const richText = document.createElement("div");
      richText.innerHTML = props.text;
      const codes: NodeListOf<HTMLElement> =
        richText.querySelectorAll("pre code");
      codes.forEach((code: HTMLElement) => {
        hljs.highlightElement(code);
      });
      return richText.innerHTML;
    });

    const clickHandler = (event: MouseEvent) => {
      const target = event.target as HTMLElement;
      console.log(target.tagName);
    };

    return {
      html,
      clickHandler,
    };
  },
});
</script>

<style lang="scss" scoped>
.components-rich-text {
  padding: 20px;
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  box-sizing: border-box;

  :deep(.rich-text) {
    width: 100%;
    height: auto;

    table {
      border-collapse: collapse;
      width: 100%;
      background-color: var(--bg-color);
      font-size: 14px;
      line-height: 1.5em;

      th,
      td {
        white-space: nowrap;
        border-top: 1px solid #dcdfe6;
        border-bottom: 1px solid #dcdfe6;
        padding: 0.6em 1em;
        text-align: left;
        max-width: 250px;
        white-space: pre-wrap;
      }
    }

    blockquote {
      margin: 0;
      padding: 1em;
      width: 100%;
      height: auto;
      border-radius: 5px;
      border-left: 6px solid #409eff;
      background: rgba($color: #409eff, $alpha: 0.2);
      box-sizing: border-box;
    }

    p {
      code {
        padding: 0.2em 0.5em;
        line-height: 1.5;
        font-size: 14px;
        color: rgb(71, 101, 130);
        font-weight: 400;
        background-color: rgb(241, 241, 241);
        border-radius: 4px;
        font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
      }
    }

    pre {
      position: relative;
      padding: 10px 20px;
      width: 100%;
      height: fit-content;
      border-radius: 10px;
      background: #282c34;
      box-sizing: border-box;

      &::before {
        content: "";
        position: absolute;
        top: 15px;
        left: 0px;
        z-index: 2;
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background: transparent;
        box-shadow: 20px 0 0px 0px #ff5f56, 45px 0 0px 0px #ffbd2e,
          70px 0 0px 0px #27c93f;
      }

      code {
        padding: 40px 0 20px 0;
        font-size: 14px;
        line-height: 1.5;
        background: #282c34;
        font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
        color: #abb2bf;
        border: none;
        box-sizing: border-box;

        &::-webkit-scrollbar {
          height: 8px;
          border-radius: 4px;
        }
        &::-webkit-scrollbar-track {
          background-color: transparent;
        }
        &::-webkit-scrollbar-thumb {
          height: 8px;
          border-radius: 4px;
          background-color: #707070;
        }
      }
    }

    hr {
      width: 100%;
      height: 2px;
      border-radius: 1px;
      background: #efefef;
      border: none;
    }
  }
}
</style>
